package com.hlx.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlx.entity.admin.RailwayAuthority;
import com.hlx.entity.admin.Station;
import com.hlx.service.admin.StationService;
import com.hlx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/station")
public class StationController {

    @Autowired
    private StationService stationService;

    @GetMapping("/getAllStationByPage/{pageSize}/{currentPage}/{railwayAbbreviation}")
    public Result<Page> getAllStationByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage,@PathVariable String railwayAbbreviation){
        //        分页构造
        log.info(railwayAbbreviation);
        LambdaQueryWrapper<Station> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Station::getBelongAuthority,railwayAbbreviation);
        Page page = new Page(currentPage,pageSize);
        return Result.success(stationService.page(page,queryWrapper));
    }

    @GetMapping("/getStationByPage/{pageSize}/{currentPage}/{railwayAbbreviation}/{search}")
    public Result<Page> getStationByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage,@PathVariable String railwayAbbreviation,@PathVariable String search){
        //        分页构造
        LambdaQueryWrapper<Station> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Station::getBelongAuthority,railwayAbbreviation);
        queryWrapper.like(Station::getStationName,search);
        Page page = new Page(currentPage,pageSize);
        return Result.success(stationService.page(page,queryWrapper));
    }

    @GetMapping("/getAllStation")
    public Result<List<Station>> getAllStation() {
        return Result.success(stationService.list());
    }
    @PostMapping("/addStation")
    public Result<String> addStation(@RequestBody Station station){
        if(stationService.save(station)){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping("/putStation")
    public Result<String> putStation(@RequestBody Station station){
        if(stationService.updateById(station)){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @DeleteMapping ("/deleteStation/{id}")
    public Result<String> deleteStation(@PathVariable Integer id){
        if(stationService.removeById(id)){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
